<template>
  <view class="container">
    <!-- 小程序头部兼容 -->
    <!-- #ifdef MP -->
    <view class="mp-header">
      <view
        class="mp-location"
        @click="getUserLocation"
      >
        <text class="location-icon yticon icon-icon-test"></text>
        <text class="location-text">{{location}}</text>
        <text class="location-xia yticon icon-xia"></text>
      </view>
      <view class="mp-search-box">
      </view>
    </view>
    
    <!-- #endif -->
		
    <!-- 头部轮播 -->
    <view class="carousel-section">
      <!-- 标题栏和状态栏占位符 -->
      <view class="titleNview-placing" />
      <!-- 背景色区域 -->
      <view
        class="titleNview-background"
        :style="{backgroundColor:titleNViewBackground}"
      />
      <swiper
        class="carousel"
        circular
        :autoplay="true"
        :indicator-dots="true"
        indicator-active-color="#fa436a"
        @change="swiperChange"
      >
        <swiper-item
          v-for="(item, index) in carouselList"
          :key="index"
          class="carousel-item"
          @click="navToGood()"
        >
          <image :src="item.src" />
        </swiper-item>
      </swiper>
    </view>
    <!-- 分类 -->
    <view class="cate-section">
      <view
        v-for="(item, index) in categoryList"
        :key="index"
        class="cate-item"
        @click="navToCategory(item, index)"
      >
        <image :src="item.image" />
        <text>{{ item.title }}</text>
      </view>
    </view>

    <!-- 热销产品 -->
    <view class="f-header m-t">
      <image src="/static/hot.png" />
      <view class="tit-box">
        <text class="tit">
          热销产品
        </text>
      </view>
      <!-- <text class="yticon icon-you" /> -->
    </view>
		
    <view class="guess-section">
      <view 
        v-for="(item, index) in goodsList"
        :key="index"
        class="guess-item"
        @click="navToDetailPage(item)"
      >
        <view class="image-wrapper">
          <image
            :src="item.goodImg"
            mode="aspectFill"
          />
        </view>
        <text class="title clamp">
          {{ item.goodName }}
        </text>
        <view class="guess-item-info">
          <text class="price">
            ￥{{ item.goodPrice }}
          </text>
        </view>
      </view>
    </view>
    <login-modal />
  </view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import LoginModal from '@/components/loginModal.vue'
export default {
  components: {
    LoginModal
  },
  data() {
    return {
      titleNViewBackground: '',
      // 当前位置信息
      location: '杭州',

      // 轮播的当前
      swiperCurrent: 0,
      swiperLength: 0,
      // 首页分类列表
      categoryList: [],
      carouselList: [],
      goodsList: []
    }
  },
  computed: {
    ...mapState(['hasLogin']),
  },
  onLoad() {
    this.loadData()
    this.useLocation()
  },
  methods: {
    ...mapMutations(['showLogin']),
    /**
     * 请求静态数据
     */
    async loadData() {
      try {
        const indexInfo = await this.$api.json('indexInfo')
        this.swiperLength = indexInfo.bannerList.length
        this.carouselList = indexInfo.bannerList
        this.titleNViewBackground = this.carouselList[0].background
        this.categoryList = indexInfo.navInfoList
          
        this.goodsList = indexInfo.hotGoodList || []
      } catch (error) {
        console.log(error)
      }
    },
    // 测试定位
    getUserLocation() {
      this.$api.msg('测试数据，不可切换')
    },
    // 获取用户定位的权限
    useLocation() {
      uni.authorize({
        scope: 'scope.userLocation',
        success(res) {
          uni.getLocation({
            type: 'wgs84',
            geocode: true,
            altitude: true,
            success(res) {
              // console.log(res)
            }
          })
        },
        fail(err) {
          console.log(err)
        }
      })
    },
    // 获取定位
    getLocation() {
      if(!this.hasLogin) {
        this.showLogin()
        return
      }
      uni.getSetting({
        success(res) {
          if(!res.authSetting['scope.userLocation']) {
            uni.openSetting({
              success(res) {
                // console.log(res.authSetting)
              }
            })
          }
          uni.navigateTo({
            url: '/pages/address/address'
          })
        },
        fail(err) {
          console.log(err)
        }
      })
    },
    // 轮播图切换修改背景色
    swiperChange(e) {
      const index = e.detail.current
      this.swiperCurrent = index
      this.titleNViewBackground = this.carouselList[index].background
    },
    // 轮播页跳转去商品详情
    navToGood() {
      this.$api.msg('功能测试中')
    },
    // 分类页
    navToCategory() {
      this.$api.msg('功能测试中')
    },
    // 详情页
    navToDetailPage(item) {
      // 测试数据没有写id，用title代替
      const id = item.goodId
      uni.navigateTo({
        url: `/pages/product/product?id=${id}`
      })
    },
  },
  // #ifndef MP
  // 标题栏input搜索框点击
  onNavigationBarSearchInputClicked: async function(e) {
    this.$api.msg('点击了搜索框')
  },
  // 点击导航栏 buttons 时触发
  onNavigationBarButtonTap(e) {
    const index = e.index
    if (index === 0) {
      this.$api.msg('点击了扫描')
    } else if (index === 1) {
      // #ifdef APP-PLUS
      // eslint-disable-next-line
      const pages = getCurrentPages()
      const page = pages[pages.length - 1]
      const currentWebview = page.$getAppWebview()
      currentWebview.hideTitleNViewButtonRedDot({
        index
      })
      // #endif
      uni.navigateTo({
        url: '/pages/notice/notice'
      })
    }
  }
  // #endif
}
</script>

<style lang="scss">
  /* #ifdef MP */
  .mp-header{
    width: 100%;
    height: 60rpx;
    // background-color: #fff;
    padding: 10rpx 20rpx;
    display: flex;
    justify-content: space-between;
  }
  .mp-location {
    display: flex;
    align-items: center;
    .location-icon {
      font-size: 32rpx;
    }
    .location-text {
      font-size: $font-sm;
      color: $font-color-base;
      margin: 0 10rpx;
    }
    .location-xia {
      font-size: $font-sm;
      color: $font-color-base;
      margin-top: 20rpx;
    }
  }
	.mp-search-box{
		.ser-input{
			flex:1;
			height: 56rpx;
			line-height: 56rpx;
			text-align: center;
			font-size: 28rpx;
			color:$font-color-base;
			border-radius: 20px;
			background: rgba(255,255,255,1);
		}
	}
	page{
		.cate-section{
			position:relative;
			z-index:5;
		}
		.carousel-section{
			padding: 0;
			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}
			.carousel{
				.carousel-item{
					padding: 0;
				}
			}
			.swiper-dots{
				left:45rpx;
				bottom:40rpx;
			}
		}
	}
	/* #endif */
	
	
	page {
		background: #f5f5f5;
	}
	.m-t{
		margin-top: 16rpx;
	}
	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;

		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 426rpx;
			transition: .4s;
		}
	}
	.carousel {
		width: 100%;
		height: 350rpx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28rpx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10rpx;
		}
	}
	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60rpx;
		bottom: 15rpx;
		width: 72rpx;
		height: 36rpx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36rpx;
			height: 36rpx;
			border-radius: 50px;
			font-size: 24rpx;
			color: #fff;
			text-align: center;
			line-height: 36rpx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36rpx;
			font-size: 12rpx;
			color: #fff;
			transform: translateX(-50%);
		}
	}
	/* 分类 */
	.cate-section {
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap:wrap;
		padding: 30rpx 22rpx; 
		background: #fff;
		.cate-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: $font-sm + 2rpx;
			color: $font-color-dark;
		}
		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 88rpx;
			height: 88rpx;
			margin-bottom: 14rpx;
			border-radius: 50%;
			opacity: .7;
			box-shadow: 4rpx 4rpx 20rpx rgba(250, 67, 106, 0.3);
		}
	}
	
	.f-header{
		display:flex;
		align-items:center;
		height: 140rpx;
		padding: 6rpx 30rpx 8rpx;
		background: #fff;
		image{
			flex-shrink: 0;
			width: 40rpx;
			height: 40rpx;
			margin-right: 20rpx;
		}
		.tit-box{
			flex: 1;
			display: flex;
			flex-direction: column;
		}
		.tit{
			font-size: $font-lg +2rpx;
			color: $font-color-dark;
			line-height: 1.3;
		}
		.tit2{
			font-size: $font-sm;
			color: $font-color-light;
		}
		.icon-you{
			font-size: $font-lg +2rpx;
			color: $font-color-light;
		}
	}
	

	/* 猜你喜欢 */
	.guess-section{
		display:flex;
		flex-wrap:wrap;
		padding: 0 30rpx;
		background: #fff;
		.guess-item{
			display:flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40rpx;
			&:nth-child(2n+1){
				margin-right: 4%;
			}
		}
		.image-wrapper{
			width: 100%;
			height: 330rpx;
			border-radius: 3px;
			overflow: hidden;
			image{
				width: 100%;
				height: 100%;
				opacity: 1;
			}
    }
    .guess-item-cart {
      width: 40rpx;
      height: 40rpx;
    }
    .guess-item-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .all-sold {
      font-size: $font-sm;
      color: $font-color-light;
    }
		.title{
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 80rpx;
		}
		.price{
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
		}
	}
	

</style>
